<?php
class MemberCashSales extends AppModel {
    var $useDbConfig  = 'mssql';
    var $useTable     = 'MemberCashSales';
    var $primaryKey   = 'MemberCashSaleID';
    var $displayField = 'InvoiceNo';
    var $name         = 'MemberCashSales';

    public function weeklyBonusReport ($filters) {
        if (empty($filters['date']) || strpos($filters['date'], ' - ') === false) {
            return false;
        }

        list($start, $end) = split(' - ', $filters['date']);

        list($day, $month, $year) = split('[/.-]', $start);
        $start = $year.'-'.$month.'-'.$day;
        list($day, $month, $year) = split('[/.-]', $end);
        $end = $year.'-'.$month.'-'.$day;

        $sql  = 'sp_ListWeeklyBonus "'.$start.'", "'.$end.'"';
        $data = $this->query($sql);
        $resp = array(
            'total_1' => 0,
            'total_2' => 0,
            'total_3' => 0,
            'total_4' => 0,
            'total_5' => 0,
            'total_6' => 0
        );

        if (count($data) == 0) {
            return 0;
        }

        foreach ($data as $i=>$r) {
            $cycleBonus       = $this->_cycleBonus($r[0]['CycleCount']);
            $weeklyTotal      = $r[0]['PSB'] + $r[0]['FSB'] + $cycleBonus + $r[0]['GB'];
            $resp['list'][$i] = array(
                'week'    => $r[0]['BonusWeek'],
                'start'   => date_format($r[0]['FromDate'], 'd/m/Y'),
                'end'     => date_format($r[0]['ToDate'], 'd/m/Y'),
                'sponsor' => $r[0]['PSB'],
                'over'    => $r[0]['FSB'],
                'cycle'   => $cycleBonus,
                'global'  => $r[0]['GB'],
                'coupon'  => $r[0]['Coupons'],
                'total'   => $weeklyTotal,
                'payment' => date_format($r[0]['BonusDate'], 'd/m/Y')
            );

            $resp['total_1'] += $r[0]['PSB'];
            $resp['total_2'] += $r[0]['FSB'];
            $resp['total_3'] += $cycleBonus;
            $resp['total_4'] += $r[0]['GB'];
            $resp['total_5'] += $r[0]['Coupons'];
            $resp['total_6'] += $weeklyTotal;
        }

        return $resp;
    } // weeklyBonusReport

    public function newMemberClubReport ($filters) {
        $date = $this->_bonusDates($filters['month']);

        if (!$date) {
            return 0;
        }

        $sql  = 'sp_ListWeeklyMemberBonus "'.$date['from'].'", "'.$date['to'].'"';
        $data = $this->query($sql);
        $resp = $memb = array();

        foreach ($data as $i=>$r) {
            $m = $r[0]['MemberCode'];
            $c = $r[0]['Cyclecount'] / 4;

            if (isset($memb[$m])) {
                $memb[$m] += $c;
            } else {
                $memb[$m] = $c;
            }
        }
        foreach ($memb as $m=>$c) {
            if ($c <= 0) {
                continue;
            }

            $resp[$m] = array(
                '3'   => '',
                '5'   => '',
                '10'  => '',
                '15'  => '',
                '20'  => '',
                '50'  => '',
                '100' => '',
                '300' => ''
            );

            if ($c < 5) {
                $resp[$m]['3'] = $c;
            } else if ($c < 10) {
                $resp[$m]['5'] = $c;
            } else if ($c < 15) {
                $resp[$m]['10'] = $c;
            } else if ($c < 20) {
                $resp[$m]['15'] = $c;
            } else if ($c < 50) {
                $resp[$m]['20'] = $c;
            } else if ($c < 100) {
                $resp[$m]['50'] = $c;
            } else if ($c < 300) {
                $resp[$m]['100'] = $c;
            } else if ($c >= 300) {
                $resp[$m]['300'] = $c;
            }
        }

        return $resp;
    } // newMemberClubReport

    public function payoutReport ($filters) {
        $sql  = 'sp_GetBonusWeekofMonth "'.$filters['month'].'-01"';
        $data = $this->query($sql);

        if (count($data) == 0) {
            return 0;
        }

        foreach ($data as $i=>$r) {
            if ($i == 0) {
                $bonusFrom = date_format($r[0]['BonusDate'], 'Y-m-d');
            } else if (count($data) == ($i+1)) {
                $bonusTo = date_format($r[0]['BonusDate'], 'Y-m-d');
            }
        }

        $sql  = 'sp_ListWeeklyMemberBonus1 "'.$bonusFrom.'", "'.$bonusTo.'", "'.$filters['country'].'"';
        $data = $this->query($sql);
        $resp = array(
            'sponsor'  => 0,
            'over'     => 0,
            'globe'    => 0,
            'cyclePay' => 0,
            'cycle'    => 0,
            'total'    => 0
        );

        if (count($data) == 0) {
            return 0;
        }

        foreach ($data as $i=>$r) {
            $m = $r[0]['MemberCode'];
            $c = ($r[0]['Cyclecount'] / 4);
            $t = $r[0]['PSB'] + $r[0]['FSB'] + $r[0]['GlobalBns'] + $r[0]['GPB'];

            if ($t == 0) { // no bonus
                continue;
            }

            $resp['sponsor']  += $r[0]['PSB'];
            $resp['over']     += $r[0]['FSB'];
            $resp['globe']    += $r[0]['GlobalBns'];
            $resp['cyclePay'] += $r[0]['GPB'];
            $resp['cycle']    += $c;
            $resp['total']    += $t;

            if (isset($resp['list'][$m])) {
                $resp['list'][$m]['sponsor']  += $r[0]['PSB'];
                $resp['list'][$m]['over']     += $r[0]['FSB'];
                $resp['list'][$m]['globe']    += $r[0]['GlobalBns'];
                $resp['list'][$m]['cyclePay'] += $r[0]['GPB'];
                $resp['list'][$m]['cycle']    += $c;
                $resp['list'][$m]['total']    += $t;
            } else {
                $resp['list'][$m] = array(
                    'sponsor'  => $r[0]['PSB'],
                    'over'     => $r[0]['FSB'],
                    'globe'    => $r[0]['GlobalBns'],
                    'cyclePay' => $r[0]['GPB'],
                    'cycle'    => $c,
                    'total'    => $t
                );
            }
        }

        return $resp;
    } // payoutReport

    public function bonusPayoutReport ($filters) {
        $date = $this->_bonusDates($filters['month']);

        if (!$date) {
            return 0;
        }

        $sql  = 'sp_ListWeeklyMemberBonus1 "'.$date['from'].'", "'.$date['to'].'", "'.$filters['country'].'"';
        $data = $this->query($sql);
        $resp = array();

        if (count($data) == 0) {
            return 0;
        }

        foreach ($data as $i=>$r) {
            $m = $r[0]['MemberCode'];
            $t = $r[0]['PSB'] + $r[0]['FSB'] + $r[0]['GlobalBns'] + $r[0]['GPB'];

            if ($t > 0) {
                if (isset($resp['list'][$m])) {
                    $resp[$m]['total'] += $t;
                    $resp[$m]['member'] += $m;
                } else {
                    $resp[$m]['total'] = $t;
                    $resp[$m]['member'] = $m;
                }
            }
        }

        return $resp;
    } // bonusPayoutReport


    private function _bonusDates ($month) {
        $sql  = 'sp_GetBonusWeekofMonth "'.$month.'-01"';
        $data = $this->query($sql);

        if (count($data) == 0) {
            return 0;
        }

        foreach ($data as $i=>$r) {
            if ($i == 0) {
                $date['from'] = date_format($r[0]['BonusDate'], 'Y-m-d');
            } else if (count($data) == ($i+1)) {
                $date['to'] = date_format($r[0]['BonusDate'], 'Y-m-d');
            }
        }

        return $date;
    } // _bonusDates

    private function _cycleBonus ($cycle) {
        return $cycle * 0.25 * 300;
    } // _cycleBonus
} // MemberCashSales
?>